package com.nova.android.tvlauncher.ui.fragment.atv

import android.R.attr.action
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.storage.StorageManager
import android.provider.Settings
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.leanback.app.GuidedStepFragment
import androidx.leanback.widget.GuidanceStylist.Guidance
import androidx.leanback.widget.GuidedAction
import com.nova.android.tvlauncher.R
import com.nova.android.tvlauncher.bean.atv_bean.Notification
import com.nova.android.tvlauncher.ui.activity.atv.StorageGuidedStepActivity
import com.shudong.lib_base.ext.appContext
import com_github_lib_base.ext.stringValue
import java.lang.reflect.Method

class StorageGuidedStepFragment : GuidedStepFragment() {

    var data: Notification.StorageDevice? = null
    override fun onCreateGuidance(savedInstanceState: Bundle?): Guidance {
        val act = activity as StorageGuidedStepActivity
        data = act.data
        val guidance = Guidance(
            data?.volumeName,
            data?.storageVolumeName,
            "", ContextCompat.getDrawable(appContext, R.drawable.app_icon_your_company)
        )
        return guidance
    }

    override fun onCreateActions(actions: MutableList<GuidedAction>, savedInstanceState: Bundle?) {
        var action = GuidedAction.Builder()
            .id(ACTION_ID_POSITIVE.toLong())
            .title(com.nova.lib_lang.R.string.notification_txt_3.stringValue()).build()
        actions.add(action)
        action = GuidedAction.Builder()
            .id(ACTION_ID_NEGATIVE.toLong())
            .title(com.nova.lib_lang.R.string.notification_txt_4.stringValue()).build()
        actions.add(action)
    }

    override fun onGuidedActionClicked(action: GuidedAction) {
        if (ACTION_ID_POSITIVE.toLong() == action?.id) {
            val intent = Intent()
            intent.action = Settings.ACTION_INTERNAL_STORAGE_SETTINGS
            startActivity(intent)
        } else {
            if (data?.storageUUID != null) {
                try {
                    val storageManager =
                        activity.getSystemService(Context.STORAGE_SERVICE) as StorageManager
                    // 反射StorageManager的unmount方法
                    val unmountMethod: Method = StorageManager::class.java.getDeclaredMethod(
                        "unmount",
                        String::class.java,
                        Boolean::class.javaPrimitiveType,
                        Boolean::class.javaPrimitiveType
                    )
                    unmountMethod.isAccessible = true
                    unmountMethod.invoke(storageManager, data?.storageUUID, true, true)
                    Toast.makeText(
                        activity, com.nova.lib_lang.R.string.notification_txt_4.stringValue(),
                        Toast.LENGTH_SHORT
                    ).show()
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
        }
        activity.finish()
    }

    companion object {
        const val ACTION_ID_POSITIVE = 1
        const val ACTION_ID_NEGATIVE = ACTION_ID_POSITIVE + 1
    }
}